async function checkToken(backend_domain_name, token) {
    const formData = new URLSearchParams();
    formData.append('token', token);

    try {
        const response = await fetch(backend_domain_name + '/ai_chat/check_token', {
            method: 'POST',
            headers: {
                'Content-Type': 'application/x-www-form-urlencoded',
            },
            body: formData
        });

        const data = await response.json();
        if (data.TF === 'T') {
            return data.username;
        } else {
            return null;
        }
    } catch (error) {
        console.error('请求错误:', error);
        return null;
    }
}
async function getAiOutput(backend_domain_name, token, user_input, historical_chat_history) {
    if (!user_input || user_input.trim() === '') {
        return {'TF': 'F', 'error': '用户输入不能为空'};
    }
    if (!historical_chat_history || historical_chat_history.trim() === '') {
        historical_chat_history = '暂无历史对话记录';
    }
    console.log(user_input);
    console.log(historical_chat_history);
    const formData = new URLSearchParams();
    formData.append('token', token);
    formData.append('user_input', user_input);
    formData.append('historical_chat_history', historical_chat_history);

    try {
        const response = await fetch(backend_domain_name + '/ai_chat/get_ai_output', {
            method: 'POST',
            headers: {
                'Content-Type': 'application/x-www-form-urlencoded',
            },
            body: formData
        });

        const data = await response.json();
        if (data.TF === 'T') {
            return data.ai_output;
        } else {
            return data.error;
        }
    } catch (error) {
        console.error('请求错误:', error);
        return '请求错误';
    }
}


